import { ConfigEnv, defineConfig, loadEnv, UserConfig } from 'vite'
import react from '@vitejs/plugin-react-swc'
import { resolve } from "path";
// https://vite.dev/config/
import { warppEnv } from "./src/utils/genv";
import viteCompression from "vite-plugin-compression";
export default defineConfig((mode:ConfigEnv):UserConfig=>{
  const env=loadEnv(mode.mode,process.cwd())
  const viteEnv=warppEnv(env)
  return{
    plugins: [
      react(),
      viteEnv.VITE_USE_COMPRESS && viteCompression(
        {
          verbose: true,
          disable: false,
          threshold: 10240,
          algorithm: "gzip",
          ext: ".gz",
          deleteOriginFile: false,
          filter:/\.(js|mjs|json|css|html)$/i,
        }
      )

    ],
  resolve: {
    alias: {
      "@": resolve(__dirname, "./src"),
    },
  },
  server:{
    host: "0.0.0.0",
    port: viteEnv.VITE_PORT,
    open: viteEnv.VITE_OPEN,
    proxy:{
      '/api': {
        target: viteEnv.VITE_API_URL,
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api/, '')
      }
    }
  },
  build:{
    outDir:'0.0.0.0',
    minify:'esbuild',
    rollupOptions:{
      output:{
        chunkFileNames: 'static/js/[name]-[hash].js',
        entryFileNames: 'static/js/[name]-[hash].js',
        assetFileNames: 'static/[ext]/[name]-[hash].[ext]'
      }
    }
  }
  }
  
})
